package com.hos.check.common.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hos.check.common.dao.GenericDAO;


@Repository
public class GenericHibernateDaoImpl<T, PK extends Serializable> implements
		GenericDAO<T, PK> {
	Class<T> type;
	private SessionFactory sessionFactory = null;

	public GenericHibernateDaoImpl() {
	}

	public GenericHibernateDaoImpl(Class<T> type) {
		this.type = type;
	}

	public Class<T> getPersistentClass() {
		return this.type;
	}

	public void delete(T persistentObject) {
		getSession().delete(persistentObject);
	}

	public void delete(PK id) {
		getSession().delete(load(id));
	}

	public T load(PK id) {
		return (T) getSession().load(this.type, id);
	}

	public T get(PK id) {
		return (T) getSession().get(this.type, id);
	}

	public PK save(T o) {
		return (PK) getSession().save(o);
	}

	public void refresh(T o) {
		getSession().refresh(o);
	}

	public void saveOrUpdate(T o) {
		getSession().saveOrUpdate(o);
	}

	public void update(T o) {
		getSession().update(o);
	}

	public Session getSession() {
		return this.sessionFactory.getCurrentSession();
	}

	public Query getQuery(String s) {
		return getSession().createQuery(s);
	}

	public SessionFactory getSessionFactory() {
		return this.sessionFactory;
	}

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public void delete(T persistentObject, Session session) {
		session.delete(persistentObject);
	}

	public void delete(PK id, Session session) {
		session.delete(load(id));
	}

	public T load(PK id, Session session) {
		return (T) session.load(this.type, id);
	}

	public T get(PK id, Session session) {
		return (T) session.get(this.type, id);
	}

	public PK save(T o, Session session) {
		return (PK) session.save(o);
	}

	public void saveOrUpdate(T o, Session session) {
		session.saveOrUpdate(o);
	}

	public void update(T o, Session session) {
		session.update(o);
	}

	public Query getQuery(String s, Session session) {
		return session.createQuery(s);
	}

	public List<T> findByCriteria(Criterion[] criterion) {
		Criteria crit = getSession().createCriteria(getPersistentClass());

		for (Criterion c : criterion) {
			crit.add(c);
		}
		return crit.list();
	}

	public List<T> findAll() {
		return findByCriteria(new Criterion[0]);
	}

	public void flush() {
		getSession().flush();
	}

	public void evict(Object obj) {
		getSession().evict(obj);
	}

	public List<T> findByExample(T exampleInstance, String[] excludeProperty) {
		Criteria crit = getSession().createCriteria(getPersistentClass());
		Example example = Example.create(exampleInstance);
		for (String exclude : excludeProperty) {
			example.excludeProperty(exclude);
		}
		crit.add(example);
		return crit.list();
	}
}
